import java.io.Console;
import java.util.Arrays;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
//import java.util.regex.Pattern;
//import java.util.regex.Matcher;
import java.util.StringTokenizer;
import java.lang.String;

public class Password {
        static BufferedReader inputStream = null;
        static BufferedReader input = null;
        static PrintWriter output = null;
   	static FileReader passwd = null;

    public static void main (String args[]) throws IOException {

        Console c = System.console();
        if (c == null) {
            System.err.println("No console.");
            System.exit(1);
        }
	try {
		if (args.length != 2){
			input = new BufferedReader(new FileReader("passwd.txt"));
			output = new PrintWriter(new FileWriter("passwd2.txt"));
		} 
		else {
			input = new BufferedReader(new FileReader(args[2]));
			output = new PrintWriter(new FileWriter(args[3]));
		}

        	String login = c.readLine("Enter your email: ");
	        char [] oldPassword = c.readPassword("Enter your old password: ");

        	if (verify(login, oldPassword)){
	            boolean noMatch;
        	    do {
                	char [] newPassword1 =
	                    c.readPassword("Enter your new password: ");
        	        char [] newPassword2 =
                	    c.readPassword("Enter new password again: ");
	                noMatch = ! Arrays.equals(newPassword1, newPassword2);
        	        if (noMatch) {
                	    c.format("Passwords don't match. Try again.%n");
	                } else {
        	            change(login, newPassword1);
                	    c.format("Password for %s changed.%n", login);
	                }
        	        Arrays.fill(newPassword1, ' ');
                	Arrays.fill(newPassword2, ' ');
	            } while (noMatch);
        	}
		else {
			System.out.println ("Email or password not found.");
		}
	} finalize {


        Arrays.fill(oldPassword, ' ');

    }

    //Dummy verify method. 
    static boolean verify(String login, char [] p) {
	String l;
	String l_login;
	String l_password;
	String password;
	password = new String(p);
	try {
		while ((l = input.readLine()) != null){
			StringTokenizer cad = new StringTokenizer(l,":");
			l_login = cad.nextToken();
			if (l_login.equals(login)){
				l_password = cad.nextToken();
				if (l_password.equals(password)){
					return true;
				}
			}
		}
	} catch (IOException ex){}
        return false;
    }

    //Dummy change method.
    static boolean change(String login, char[] p) {
	String l;
	String l_login;
	String l_password;
	String password;
	password = new String(p);
	try {
		while ((l = input.readLine()) != null){
			StringTokenizer cad = new StringTokenizer(l,":");
			l_login = cad.nextToken();
			if (l_login.equals(login)){
				l_password = cad.nextToken();
				if (l_password.equals(password)){
					output.println(login);
					return true;
				}
			}
		}
	} catch (IOException ex){}
        return false;
    }
}
